import { pathToBase64, base64ToPath } from '@/js_sdk/gsq-image-tools/image-tools/index.js'
export default {
	data () {
		return {
			data: {},
			url: '',
			imgList:[],
			delImgArr:[],
			tags:[],
		}
	},
	onLoad(){
		const _this = this;
		_this.$api.findByHealthId().then((res)=>{
			_this.data = res.data.result;
			_this.imgList = res.data.result.images || []
			uni.createSelectorQuery().select('#editor').context((a) => {this.editorCtx = a.context; this.editorCtx.setContents({html: res.data.result && res.data.result.individual || ''})}).exec();
			_this.$api.DataDictfindByType({type:'labelConfiguration'}).then((res2)=>{
				_this.tags = res2.data.result.map((a)=>{
					a.checked = false
					const labelIdarr = _this.data.labelId && _this.data.labelId.split(',')
					labelIdarr && labelIdarr.map((b)=>{
						if(a.id == b){
							a.checked = true
						}
					})
					return a
				})
			})
		})
	},
	methods:{
		onEditorReady(){
			uni.createSelectorQuery().select('#editor').context((res) => {
			  this.editorCtx = res.context
			}).exec()
		},
		// 添加证书
		addImage(){
			const _this = this;
			uni.chooseImage({
				success: (res) => {
					const file = res.tempFiles.map((a)=>{
						_this.$api.uploadImg({file:a,type:20}).then((ares)=>{
							if(ares.returnCode == 200){
								_this.imgList.push(ares.result.url)
							}
						})
					})
					
				}
			});
		},
		// 修改头像
		chooseImage() {
			// const _this = this;
			// uni.chooseImage({
			// 	count:1,
			// 	success: (res) => {
			// 		_this.$api.uploadImg({file:res.tempFiles[0],type:20}).then((response)=>{
			// 			if(response.returnCode == 200){
			// 				_this.data.headPicture = response.result.url
			// 			}
			// 		})
			// 	}
			// });
			uni.navigateTo({
				url:'/pages/headFeatures/changeAvatar/index?headPicture='+ (this.data && this.data.headPicture ? this.data.headPicture : '../../../static/img/defalut_tx.png')
			})
		},
		delimg(item,index){
			const _this = this;
			_this.imgList.splice(index,1)
			_this.delImgArr.push(item)
		},
		save(){
			const _this = this;
			_this.delImgArr.map((a)=>{
				_this.$api.delUrl(a.substr(44)).then((response)=>{})
			})
			var labelId = ''
			this.tags.map((a)=>{
				if(a.checked){
					labelId += a.id+','
				}
			})
			const data = {
				appellations: this.data.appellations,
				headPicture: this.data.headPicture,
				images: this.imgList,
				individual: this.data.individual && this.data.individual.detail ? this.data.individual.detail.html : this.data.individual,
				phone: this.data.phone,
				department: this.data.department,
				labelId:labelId.substr(0,labelId.length-1)
			}
			_this.$api.updateHealth(data).then(res=>{
				if(res.data.returnCode == 200){
					uni.navigateTo({
						url:'/pages/index/index'
					})
				}
			})
		},
		changeTag(item){
			item.checked = !item.checked
		}
	}
}